/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wangjie.wheelview.slice;

import com.wangjie.wheelview.LogUtil;
import com.wangjie.wheelview.WheelView;
import com.wangjie.wheelview.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;

import java.util.Arrays;

/**
 * Main ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * * The constant PLANETS
     */
    private static final String[] PLANETS = new String[]{"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Uranus", "Neptune", "Pluto"};
    /**
     * The constant Wheel view
     */
    private WheelView wheelView;

    /**
     * The constant Common dialog
     */
    private CommonDialog commonDialog;

    /**
     * On start *
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        wheelView = (WheelView) findComponentById(ResourceTable.Id_wv_main);
        wheelView.setOffset(1);
        wheelView.setItems(Arrays.asList(PLANETS));
        wheelView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                LogUtil.info("MainAbilitySlice", "selectedIndex: " + selectedIndex + ", item: " + item);
            }
        });

        findComponentById(ResourceTable.Id_btn_main_show_dialog).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Component outerView = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_wheel_view, null, false);
                WheelView wv = (WheelView) outerView.findComponentById(ResourceTable.Id_wv_main);
                wv.setOffset(2);
                wv.setItems(Arrays.asList(PLANETS));
                wv.setSeletion(3);
                wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
                    @Override
                    public void onSelected(int selectedIndex, String item) {
                        LogUtil.info("MainAbilitySlice", "selectedIndex: " + selectedIndex + ", item: " + item);
                    }
                });

                commonDialog = new CommonDialog(getContext());
                commonDialog.setTitleText("WheelView in Dialog")
                        .setContentCustomComponent(outerView)
                        .setAlignment(LayoutAlignment.CENTER)
                        .setSize(1000, 1000)
                        .show();
                commonDialog.setDialogListener(new BaseDialog.DialogListener() {
                    @Override
                    public boolean isTouchOutside() {
                        commonDialog.hide();
                        return false;
                    }
                });
            }
        });
    }

    /**
     * On active
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * On foreground *
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
